package grammar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    @FXML
    Button runBt;
    @FXML
    Button exitBt;
    @FXML
    TextArea inputTextArea;
    @FXML
    TreeView<String> tree;

    @FXML
    void run() {
        String text = inputTextArea.getText();
        String path = this.getClass().getResource("/").getPath();
        File code = new File(path, "input.code");
        File result = null;
        Writer writer = null;
        Reader reader = null;
        StringBuilder json = new StringBuilder();
        try {
            writer = new FileWriter(code);
            writer.write(text);
            writer.flush();
            writer.close();
            String[] cmd = {
                    "cmd",
                    "/c",
                    "grammar2json"
            };
            Process process = new ProcessBuilder().command(cmd).directory(new File(path)).start();
            process.waitFor();
            result = new File(path, "grammar.json");
            reader = new FileReader(result);
            char[] chars = new char[1024];
            while (reader.read(chars) != -1) {
                json.append(chars);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        Map maps = (Map) JSON.parse(json.toString());

        tree.getRoot().getChildren().addAll(buildTree(maps));
    }

    List<TreeItem<String>> buildTree(Map maps) {
        List<TreeItem<String>> rootItems = new ArrayList<>();
        for (Object map : maps.entrySet()) {
            Map.Entry entry = (Map.Entry) map;
//            System.out.println(entry.getKey() + "     " + entry.getValue());
            Object val = entry.getValue();
            TreeItem<String> item = null;
            if (val instanceof JSONObject) {
                item = new TreeItem<>((String) entry.getKey());
                List<TreeItem<String>> list = buildTree((Map) val);
                item.getChildren().addAll(list);
            } else if (val instanceof JSONArray) {
                item = new TreeItem<>((String) entry.getKey());
                for (Object obj : (JSONArray) val) {
                    item.getChildren().addAll(buildTree((Map) obj));
                }
            } else {
                item = new TreeItem<>((String) val);
            }
            rootItems.add(item);
        }
        return rootItems;
    }


    @FXML
    void exit() {
        Platform.exit();
    }

    @FXML
    void clear() {
        inputTextArea.setText("");
        tree.setRoot(new TreeItem<>("语法树"));
    }

    @FXML
    void run2() {

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tree.setRoot(new TreeItem<String>("语法树"));
    }
}
